using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

namespace View.Instant
{
    public class GongxihuodeView : View
    {
        private Button mbtnClose;
        private GameObject mItem;
        private List<GameObject> mObjectList = new List<GameObject>();
        public override void OnLoad()
        {
            base.OnLoad();
            
            mbtnClose = transform.Find("btnClose").GetComponent<Button>();
            mItem = transform.Find("gameobj/Item").gameObject;

            AddClickListener(mbtnClose,() =>
            {
                OnDestroyObj();
                UIManager.Instance.Close(ViewName.GongxihuodeView);
            });
        }

        private void OnCreateObj(object[] args)
        {
            List<PropItemEnum> itemenumlist = (List<PropItemEnum>)(args[0]);
            for (int i = 0; i < itemenumlist.Count; i++)
            {
                var propitemdata = GameLocalInfo.OnGetPropItemTableById(itemenumlist[i]);
                GameObject go = GameObject.Instantiate(mItem);
                go.transform.SetParent(mItem.transform.parent);
                go.transform.localPosition = Vector3.zero;
                go.transform.localScale = Vector3.one;
                go.transform.localRotation = Quaternion.identity;
                go.SetActive(true);

                var spriteimg = go.transform.Find("Image").GetComponent<Image>();
                spriteimg.sprite = ResourceManager.Instance.LoadSprite(propitemdata.path, propitemdata.iconname);
                spriteimg.SetNativeSize();
                go.transform.Find("txtnum").GetComponent<Text>().text = string.Format("x{0}",propitemdata.count);
                go.transform.Find("title").GetComponent<Text>().text = propitemdata.itemname;
                
                mObjectList.Add(go);
            }
        }
        private void OnDestroyObj()
        {
            for (int i = 0; i < mObjectList.Count; i++)
            {
                GameObject.Destroy(mObjectList[i]);
            }
            mObjectList.Clear();
        }

        public override void OnShow(params object[] args)
        {
            base.OnShow();
            OnCreateObj(args);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            if (gameObject.activeSelf)
            {
                OnDestroyObj();
                // transform.localScale = Vector3.zero;
            }
        }
    }
}

